package com.nervenets.web.controller.handler.user.user;

import com.nervenets.web.controller.handler.Handler;
import com.nervenets.web.controller.handler.user.UserModel;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.hibernate.domain.user.User;

import javax.servlet.http.HttpServletRequest;

public class UserLoginHandler extends Handler<UserLoginParams> {
    @Override
    public String handle(UserLoginParams params, HttpServletRequest request) throws Exception {
        User userByMobile = userDao.getUserByMobile(params.getMobile());
        if (null == userByMobile) return errorMessage(MessageCode.code_404, "输入账号或密码不正确");

        if (!params.getPassword().equals(userByMobile.getPassword()))
            return errorMessage(MessageCode.code_444, "输入账号或密码不正确");

        UserModel userModel = new UserModel(userByMobile, 1, globalVariablesService.getGlobalRule().getDefaultAvatar());
        return successMessage(userModel);
    }

    @Override
    public String result() throws Exception {
        return successMessage();
    }
}
